package com.ls.lserp.modules.trade.service;



import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.dbaccess.Dao.ExchangeFormEntityMapper;
import com.ls.lserp.dbaccess.Dao.FinanceEntityMapper;
import com.ls.lserp.dbaccess.Dao.FinanceInvoiceEntityMapper;
import com.ls.lserp.dbaccess.Dao.VoucherEntityMapper;
import com.ls.lserp.dbaccess.Entity.FinanceEntity;
import com.ls.lserp.dbaccess.Entity.FinanceItemEntity;
import com.ls.lserp.dbaccess.Entity.VoucherDetailEntity;
import com.ls.lserp.dbaccess.Entity.VoucherEntity;
import com.ls.lserp.modules.common.entity.UploadFile;
import com.ls.lserp.modules.order.dao.AuditInfoDao;
import com.ls.lserp.modules.order.dao.OrderDao;
import com.ls.lserp.modules.sys.dao.UploadFileDao;
import com.ls.lserp.modules.trade.dao.SettlementDao;
import com.ls.lserp.modules.trade.entity.AccountVoucher;

@Service
@Transactional(readOnly = true)
public class AccountingVoucherService extends CrudService<VoucherEntityMapper, AccountVoucher>{
	
    @Autowired
    private FinanceEntityMapper financeEntityMapper;
    @Autowired
    private VoucherEntityMapper voucherEntityMapper;
    @Autowired
    private UploadFileDao uploadFileDao;
    /**
	  * @Title: findList
	  * @Description: 一览查询
	  * @return Page<VoucherEntity> 
	  */
    public Page<VoucherEntity> findList(Page<VoucherEntity> page, VoucherEntity voucherEntity) {
        // 设置分页参数
    	voucherEntity.setPage(page);
        // 执行分页查询
        page.setList(voucherEntityMapper.findList(voucherEntity));
        return page;
    }
    /**
	  * @Title: findUnrecordedList
	  * @Description: 一览查询审核未入账数据
	  * @return void 
	  * @throws
	  */
    public Page<FinanceEntity> findUnrecordedList(Page<FinanceEntity> page, FinanceEntity financeEntity) {
    	financeEntity.setTabPageStatus(3);
    	financeEntity.setAccountsStatus(ResourcesUtil.getConfig("financeStatus02"));
		financeEntity.setAuditStatus(ResourcesUtil.getConfig("reserver_meterial_ok"));
        // 设置分页参数
    	financeEntity.setPage(page);
        // 执行分页查询
        page.setList(financeEntityMapper.findList(financeEntity));
        return page;
    }
    /**
     * 获取会计科目数据集
     * @Title: findFinanceItem
     * @return List<FinanceItemEntity>
     */
    public List<FinanceItemEntity> findFinanceItem(){
    	
    	return voucherEntityMapper.findFinanceItem();
    }
    
    /**
     * 获取一条数据信息
     * @Title: getInfo
     * @return AccountVoucher
     */
    public AccountVoucher getInfo(VoucherEntity voucherEntity){
    	//获取凭证信息
    	AccountVoucher accountVoucher = voucherEntityMapper.get(voucherEntity);
    	//获取凭证明细信息
    	List<VoucherDetailEntity> voucherDetailList = voucherEntityMapper.getDetail(voucherEntity);
    	accountVoucher.setVoucherDetailEntity(new VoucherDetailEntity());
    	accountVoucher.getVoucherDetailEntity().setVoucherDetailList(voucherDetailList);
    	return accountVoucher;
    }
    
    /**
     * 获取一条数据信息
     * @Title: getInfo
     * @return AccountVoucher
     */
    public VoucherEntity checkExist(VoucherEntity voucherEntity){
    	//获取凭证信息
    	voucherEntity = voucherEntityMapper.getByVoucherNo(voucherEntity);
    	//获取凭证明细信息
    	return voucherEntity;
    }
    
    /**
     * 获取历史凭证
     * @Title: findVoucherHistory
     * @return List<VoucherDetailEntity>
     */
    public List<VoucherDetailEntity> findVoucherHistory(AccountVoucher accountVoucher) throws Exception{
    	List<VoucherDetailEntity> vDetailList = new ArrayList<>();
    	if (!"".equals(accountVoucher.getFinanceId())) {
			String[] financeIdStr = accountVoucher.getFinanceId().split(",");
			for (int i = 0; i < financeIdStr.length; i++) {
				accountVoucher.setFinanceId(financeIdStr[i]);
				vDetailList.addAll(voucherEntityMapper.findVoucherHistory(accountVoucher));
			}
		}
    	return vDetailList;
    }
    
    /**
     * 逻辑删除凭证
     * @Title: deleteVoucher
     *
     */
    @Transactional(readOnly = false)
    public void deleteVoucher(AccountVoucher accountVoucher) throws Exception{
    	//逻辑删除凭证
    	accountVoucher.preUpdate();
    	voucherEntityMapper.delete(accountVoucher);
    	if (accountVoucher.getVoucherDetailEntity().getVoucherDetailList() != null 
				&& accountVoucher.getVoucherDetailEntity().getVoucherDetailList().size() > 0 ) {
			//将业务结算状态还原为未入账
			for (VoucherDetailEntity vEntity : accountVoucher.getVoucherDetailEntity().getVoucherDetailList()) {
				vEntity.setAccountsStatus(ResourcesUtil.getConfig("financeStatus02"));
				voucherEntityMapper.updateFinanceStatus(vEntity);
			}
		}
    	
    }
    
	/**
	  * @Title: saveData
	  * @Description: 保存
	  * @param @param accountVoucher
	  * @return void 
	  * @throws
	  */
	@Transactional(readOnly = false)
	public void saveData(AccountVoucher accountVoucher) throws Exception{
		//编辑保存
		if (accountVoucher.getVoucherEntity().getVoucherId() != null) {
			accountVoucher.preInsert();
			UploadFile uploadFile = accountVoucher.getUploadFile();
			uploadFile.preUpdate();
			uploadFileDao.updateUploadFile(uploadFile);
			accountVoucher.getVoucherEntity().preUpdate();
			voucherEntityMapper.updateData(accountVoucher.getVoucherEntity());
			accountVoucher.getVoucherDetailEntity().setVoucherId(accountVoucher.getVoucherEntity().getVoucherId());
			accountVoucher.getVoucherDetailEntity().preInsert();
			voucherEntityMapper.insertDetailData(accountVoucher.getVoucherDetailEntity());
		//新建保存
		}else {
			accountVoucher.preInsert();
			UploadFile uploadFile = accountVoucher.getUploadFile();
			if (!"".equals(uploadFile.getFilePath())&&!"".equals(uploadFile.getUploadFile())) {
				uploadFile.preInsert();
				uploadFile.setFileKbn(1);
				uploadFile.setModuleKbn(6L);
				uploadFile.setFileSource(1L);
				uploadFile.setFtpFile("/accountVoucherFile");
				uploadFileDao.saveUploadFile(uploadFile);
				accountVoucher.getVoucherEntity().setVoucherFileId(accountVoucher.getUploadFile().getFileId());
			}
			accountVoucher.getVoucherEntity().preInsert();
			voucherEntityMapper.insertData(accountVoucher.getVoucherEntity());
			accountVoucher.getVoucherDetailEntity().setVoucherId(accountVoucher.getVoucherEntity().getVoucherId());
			accountVoucher.getVoucherDetailEntity().preInsert();
			voucherEntityMapper.insertDetailData(accountVoucher.getVoucherDetailEntity());
			if ("1".equals(accountVoucher.getSaveFlag())) {
				if (accountVoucher.getVoucherDetailEntity().getVoucherDetailList() != null 
						&& accountVoucher.getVoucherDetailEntity().getVoucherDetailList().size() > 0 ) {
					//更新业务结算状态为已入账
					for (VoucherDetailEntity vEntity : accountVoucher.getVoucherDetailEntity().getVoucherDetailList()) {
						vEntity.setAccountsStatus(ResourcesUtil.getConfig("financeStatus01"));
						voucherEntityMapper.updateFinanceStatus(vEntity);
					}
				}
			}
		}
	}


}
